/**
 * date: 2018年2月23日
 * 
 * js循环遍历节点,并将纯文本包装(wrap)
 * 
 * [如何引入jquery] https://zhidao.baidu.com/question/588672774551476565.html
 * 
 * 
 */
class LoopInlineNodes{
    private regexp = new RegExp("(<.+?>).+(<\/.+?>)");
    private htmlStr: string = '';
    // public constructor(){
    // }
    public getHtmlStr(){
        return this.htmlStr;
    }

    private loop(ele:any){
        if(ele.nodeType === 3){
            let len:number = ele.length;
            for(let i=0; i<len; i++){
                this.htmlStr += '<span class="emr">' + ele[i] + '</span>';
            }
        }else{
            let startTag:string = '';
            let endTag:string = '';
            if( this.regexp.test( $(ele)[0].outerHTML ) ){
                startTag = RegExp.$1;
                endTag = RegExp.$2;
            }
            this.htmlStr += startTag;
            let contentsArr:any = $(ele).contents();
            if(contentsArr && contentsArr.length > 0){
                $.each(contentsArr, function(i,n){
                    this.loop( n );
                });
            }
            this.htmlStr += endTag;
        }
    }
}
function $(ele:any){
    return {
        contents:function(){},
        each: function (a:any, b:any) { }
    };
}